package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.CoserPicture;
import com.slg.android.manyanime.utils.DataStore;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Darker on 2015/3/10.
 */
public class CoserAdapter extends RecyclerView.Adapter<CoserAdapter.ViewHolder> implements View.OnTouchListener{
    private Context context;
    private List<CoserPicture> list;
    private View.OnClickListener listener;


    public CoserAdapter(Context context, ArrayList<CoserPicture> list) {
        this.context = context;
        this.list = list;
        if(context instanceof View.OnClickListener){
            listener = (View.OnClickListener)context;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(
                R.layout.item_coser_water_full_cardview, viewGroup, false);
        view.setOnTouchListener(this);
        if(listener != null){
            view.setOnClickListener(listener);
        }
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {

        CoserPicture coserPicture = list.get(i);
        if(coserPicture != null){

            viewHolder.imgName.setText(coserPicture.getName());
            viewHolder.zanNum.setText(coserPicture.getNum());
            //设置加载中和失败的图片
            BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
            bitmapUtils.configDefaultLoadFailedImage(R.drawable.cartoon_hot_loading);
            bitmapUtils.configDefaultLoadingImage(R.drawable.cartoon_hot_loading);
            bitmapUtils.display(viewHolder.imageView, coserPicture.getPic());

        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        CardView card = (CardView)v;
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                card.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.cardview_default_elevation));
                break;
            case MotionEvent.ACTION_UP:
                break;
            case MotionEvent.ACTION_CANCEL:
                card.setCardElevation(context.getResources().getDimensionPixelSize(R.dimen.cardview_default_radius));
                break;
        }
        //当CardView有OnClick事件时返回false,没有时返回true
        return listener == null;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        @ViewInject(R.id.item_coser_picture_image)
        private ImageView imageView;
        @ViewInject(R.id.item_coser_picture_name)
        private TextView imgName;
        @ViewInject(R.id.item_coser_picture_zan_num)
        private TextView zanNum;
        public ViewHolder(View itemView) {
            super(itemView);
            ViewUtils.inject(this,itemView);
        }
    }

}
